package com.snailcatmall.www.tools;

import com.snailcatmall.www.callback.CallBackData;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AllTools {
    //    文件夹递归
    public static void recursiveTraversal(File file, CallBackData callBackData) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    recursiveTraversal(f,callBackData);
                }
            }
        } else {
            callBackData.dataPath(file.getAbsolutePath());
        }
    }
    public static boolean cutFile(File sourceFile, File destDir) {
        if (sourceFile.exists() && sourceFile.isFile() && destDir.exists() && destDir.isDirectory()) {
            File destFile = new File(destDir, sourceFile.getName());
            return sourceFile.renameTo(destFile);
        }
        return false;
    }
    public static void readFile(File file,CallBackData callBackData)  {
        try {
            InputStream inputStream = new FileInputStream(file);
            String vals = readInputStream(inputStream);
            callBackData.dataText(vals);
            inputStream.close();
        } catch (Exception e) {
            System.err.println("文件找不到读写失败了");
        }
    }
    public static String readInputStream(InputStream inputStream)  {
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, 5 * 1024 * 1024);
            String str = "";
            int item = bufferedInputStream.read();
            if (item == -1) {
                return null;
            } else {
                str = str + "" + (char) item;
                int len = bufferedInputStream.available();
                if (len > 0) {
                    byte[] dats = new byte[1024*50];
                    bufferedInputStream.read(dats);
                    str = str + new String(dats, "UTF-8");
                }
                inputStream.close();
                bufferedInputStream.close();
                return new String(str.getBytes("UTF-8"), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void readTextFile(File file,CallBackData callBackData) {
        try (FileReader reader = new FileReader(file)) {
            char[] buffer = new char[1024 * 50];
            reader.read(buffer);
            callBackData.dataText(String.valueOf(buffer),reader);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建一个文件路径对象
//        Path path = Paths.get("F:\\BaiduNetdiskDownload\\novelai-webui一键包\\novelai-webui-aki-v2\\models\\Stable-diffusion\\RubyLL_v1.safetensors");
//
//        // 读取文件的前 50KB 内容并将其转换为二进制字符串
//        try (InputStream is = Files.newInputStream(file.toPath())) {
//            byte[] buffer = new byte[1024*50];
//            String text = new String(buffer, Charset.forName("UTF-8"));
//            callBackData.dataText(text,is);
//            // 处理二进制字符串
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }




}
